package me.jzn.core.utils;

/**
 * @author xqs @date 2019/7/27 注意:base64最后等号可以去掉，不影响解码,因此本工具就把最后的等号去掉了。
 */
public class Base64Util {
    /**
     * symbol: +/
     */
    public static String encode(byte[] bytes, boolean hasPadding) {
        int flag = android.util.Base64.NO_WRAP;
        if (!hasPadding) {
            flag |= android.util.Base64.NO_PADDING;
        }
        return android.util.Base64.encodeToString(bytes, flag);
    }

    public static byte[] decode(String str) {
        int flag = android.util.Base64.NO_WRAP;
        return android.util.Base64.decode(str, flag);
    }

    /**
     * defaut is the MIME format, has the CRLF every 74 chars. should use carefully
     */
    @Deprecated
    public static final String encodeMime(byte[] bytes) {
        int flag = android.util.Base64.DEFAULT;
        return android.util.Base64.encodeToString(bytes, flag);
    }

    @Deprecated
    public static byte[] decodeMime(String str) {
        int flag = android.util.Base64.DEFAULT;
        return android.util.Base64.decode(str, flag);
    }

    /**
     * symbol: _-
     */
    public static String encodeUrl(byte[] bytes, boolean hasPadding) {
        int flag = android.util.Base64.NO_WRAP | android.util.Base64.URL_SAFE;
        if (!hasPadding) {
            flag |= android.util.Base64.NO_PADDING;
        }
        return android.util.Base64.encodeToString(bytes, flag);
    }

    public static byte[] decodeUrl(String str) {
        int flag = android.util.Base64.DEFAULT | android.util.Base64.NO_WRAP | android.util.Base64.URL_SAFE;
        return android.util.Base64.decode(str, flag);
    }
}
